var PerformanceTable = { // 業績明細初始化
    id: "PerformanceTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

PerformanceTable.initColumn = function () { // 初始化表格的列
    var columns = [
        {field: 'selectItem', radio: true},
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: 'customerId', field: 'customerId', visible: false, align: 'center', valign: 'middle'},
        {title: 'reservationsId', field: 'reservationsId', visible: false, align: 'center', valign: 'middle',},
        {title: 'equation', field: 'equation', visible: false, align: 'center', valign: 'middle',},
        {title: '欠费变动', field: 'accountArrears2', visible: false, align: 'center', valign: 'middle'},
        {title: '储值抵扣', field: 'depositAmount', visible: false, align: 'center', valign: 'middle'},
        {title: '病历号', field: 'medicalRecordNumber', align: 'center', valign: 'middle', sortable: true, width: 140},
        {title: '客户姓名', field: 'customerName', align: 'center', valign: 'middle', sortable: true, width: 120},
        {title: '电话', field: 'customerPhone', align: 'center', valign: 'middle', sortable: true, width: 140},
        {
            title: '性别', field: 'customerSex', align: 'center', valign: 'middle', sortable: true, width: 90,
            formatter: function (value, row, index) {
                if (value == 1) {
                    return "男"
                } else if (value == 2) {
                    return "女"
                } else if (value == 3) {
                    return "未知"
                }
            }
        },
        {title: '年龄', field: 'customerAge', align: 'center', valign: 'middle', sortable: true, width: 90},
        {
            title: '到店时间', field: 'arrivalTime', align: 'center', valign: 'middle', sortable: true, width: 180,
            formatter: function (value, row, index) {
                return value.substring(0, 19);
            }
        },
        {
            title: '结账时间', field: 'rechargeTime', align: 'center', valign: 'middle', sortable: true, width: 180,
            formatter: function (value, row, index) {
                return value.substring(0, 19);
            }
        },
        {title: '新老客', field: 'customerStatus', align: 'center', valign: 'middle', sortable: true, width: 100},
        {
            title: '交易类型', field: 'isDo', align: 'center', valign: 'middle', sortable: true, width: 120,
            formatter: function (value, row, index) {
                if (value == 3) {
                    return "还欠款"
                } else if (value == 4) {
                    return "退款"
                } else if (value == 5) {
                    return "消费"
                } else if (value == 9) {
                    return "储值变动"
                } else if (value == 10) {
                    return "种植牙俱乐部缴费"
                } else {
                    return "其他"
                }
            }
        },
        {title: '科室', field: 'treatmentUnit', align: 'center', valign: 'middle', sortable: true, width: 100},
        {
            title: '应收', field: 'foldingPay', align: 'center', valign: 'middle', sortable: true, width: 90,
            formatter: function (value, row, index) {
                if (row.isDo == 5) {  //财务应收=应收-本次优惠抵扣
                    return (parseFloat(row.serviceActualPay) + parseFloat(row.accountArrears2) + parseFloat(row.deductionAmount) + parseFloat(row.depositAmount)).toFixed(2);
                } else if (row.isDo == 3) {   //财务应收=0
                    return 0;
                } else if (row.isDo == 4) {  //财务应收=实收+欠费变动
                    return (parseFloat(row.serviceActualPay) + parseFloat(row.accountArrears2) + parseFloat(row.deductionAmount) + parseFloat(row.depositAmount)).toFixed(2);
                } else if (row.isDo == 9 || row.isDo == 10) {  //财务应收=实收
                    return 0;
                }
            }
        },
        {
            title: '实收', field: 'serviceActualPay', align: 'center', valign: 'middle', sortable: true, width: 90,
            formatter: function (value, row, index) {
                if (row.isDo == 5) {  //实收=实付
                    return (parseFloat(row.serviceActualPay)).toFixed(2);
                } else if (row.isDo == 3) {   //实收（现金流）=本次实付
                    return (parseFloat(row.serviceActualPay)).toFixed(2);
                } else if (row.isDo == 4) {  //实收=-实收；
                    return parseFloat(row.serviceActualPay).toFixed(2);
                } else if (row.isDo == 9 || row.isDo == 10) {  //实收=实收 储值变动
                    return value;
                }
            }
        },
        {
            title: '实到金额', field: 'serviceActualPay', align: 'center', valign: 'middle', sortable: true, width: 110,
            formatter: function (value, row, index) {
                if (row.isDo == 5) {  //实收=实付
                    return (parseFloat(row.serviceActualPay)*(row.equation/100)).toFixed(2);
                } else if (row.isDo == 3) {   //实收（现金流）=本次实付
                    return (parseFloat(row.serviceActualPay)*(row.equation/100)).toFixed(2);
                } else if (row.isDo == 4) {  //实收=-实收；
                    return parseFloat(row.serviceActualPay)*(row.equation/100).toFixed(2);
                } else if (row.isDo == 9 || row.isDo == 10) {  //实收=实收 储值变动
                    return value*(row.equation/100);
                }
            }
        },
        {title: '储值划扣', field: 'depositAmount', align: 'center', valign: 'middle', sortable: true, width: 120},
        {title: '优惠抵扣', field: 'deductionAmount', align: 'center', valign: 'middle', sortable: true, width: 120},
        {
            title: '全院业绩', field: 'hospitalPerformance', align: 'center', valign: 'middle', sortable: true, width: 120,
            formatter: function (value, row, index) {
                if (row.isDo == 4) {  //退费 实收=实收
                    return parseFloat(row.serviceActualPay).toFixed(2);
                }else{
                    return (parseFloat(row.serviceActualPay)).toFixed(2);
                }
            }
        },
        {
            title: '科室业绩', field: 'sectionPerformance', align: 'center', valign: 'middle', sortable: true, width: 120,
            formatter: function (value, row, index) {
                if (row.isDo == 9 || row.isDo == 10) {
                    return 0;
                }else{
                    return (parseFloat(row.serviceActualPay) + parseFloat(row.depositAmount)).toFixed(2);
                }
            }
        },
        {title: '一级渠道', field: 'oneChannel', align: 'center', valign: 'middle', sortable: true, width: 120},
        {title: '二级渠道', field: 'twoChannel', align: 'center', valign: 'middle', sortable: true, width: 120},
        {title: '三级渠道', field: 'thrChannel', align: 'center', valign: 'middle', sortable: true, width: 120},
        {title: '一级媒介', field: 'oneMedium', align: 'center', valign: 'middle', sortable: true, width: 120},
        {title: '二级媒介', field: 'twoMedium', align: 'center', valign: 'middle', sortable: true, width: 120},
        {title: '三级媒介', field: 'thrMedium', align: 'center', valign: 'middle', sortable: true, width: 120},
        {title: 'TC', field: 'tcName', align: 'center', valign: 'middle', sortable: true, width: 90},
        {title: '推荐人', field: 'referee', align: 'center', valign: 'middle', sortable: true, width: 100},
        {title: '护士', field: 'nurse', align: 'center', valign: 'middle', sortable: true, width: 90},
        {title: '门店', field: 'outpatientName', align: 'center', valign: 'middle', sortable: true, width: 110},
        {
            title: '网电咨询师', field: 'scarchiving', align: 'center', valign: 'middle', sortable: true, width: 130,
            formatter: function (value, row, index) {
                if (value == "null") {
                    return "";
                } else {
                    return value;
                }
            }
        },
        {title: '正畸编号', field: 'orthodonticNumber', align: 'center', valign: 'middle', sortable: true, width: 110},
        {title: '转诊人', field: 'referralerPerson', align: 'center', valign: 'middle', sortable: true, width: 90},
        {title: '被转诊人', field: 'referralerPersoned', align: 'center', valign: 'middle', sortable: true, width: 110},
        {title: '转诊时间', field: 'referralerTime', align: 'center', valign: 'middle', sortable: true, width: 180},
    ];
    return columns;
};

PerformanceTable.check = function () { // 检查是否选中
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        PerformanceTable.seItem = selected[0];
        return true;
    }
};

PerformanceTable.search = function () { // 查询业绩明细列表
    PerformanceTable.table.refresh({query: PerformanceTable.formParams()});
    PerformanceTable.total();
};

PerformanceTable.formParams = function () { // 查询表单提交参数对象  @returns {{}}
    var queryData = {};
    queryData['reach'] = parent.$("#doctorTcInputPublics").val();
    queryData['startRechargeTime'] = $("#startRechargeTime").val();
    queryData['endRechargeTime'] = $("#endRechargeTime").val();
    queryData['oneChannel'] = multipleSelection($('#oneChannel').val());
    queryData['twoChannel'] = multipleSelection($('#twoChannel').val());
    queryData['thrChannel'] = multipleSelection($('#thrChannel').val());
    queryData['oneMedium'] = multipleSelection($('#oneMedium').val());
    queryData['twoMedium'] = multipleSelection($('#twoMedium').val());
    queryData['thrMedium'] = multipleSelection($('#thrMedium').val());
    queryData['scarchiving'] = multipleSelection($('#scarchiving').val());
    queryData['tcName'] = multipleSelection($('#tcName').val());
    queryData['isDoName'] = $('#isDoName').val();
    queryData['treatmentUnit'] = multipleSelection($('#treatmentUnit').val());
    queryData['customerStatus'] = multipleSelection($('#customerStatus').val());
    queryData['outpatientName'] = $('#outpatientName',parent.document).val();
    return queryData;
};

PerformanceTable.resetSearch = function () { // 重置
    parent.$("#doctorTcInputPublics").val("");
    $("#startRechargeTime").val("");
    $("#endRechargeTime").val("");
    $("#scarchiving").selectpicker('deselectAll');
    $("#tcName").selectpicker('deselectAll');
    $("#isDoName").val("");
    $("#treatmentUnit").selectpicker('deselectAll');
    $("#customerStatus").selectpicker('deselectAll');
    $("#oneChannel").selectpicker('deselectAll');
    $("#twoChannel").selectpicker('deselectAll');
    $("#thrChannel").selectpicker('deselectAll');
    $("#oneMedium").selectpicker('deselectAll');
    $("#twoMedium").selectpicker('deselectAll');
    $("#thrMedium").selectpicker('deselectAll');
    $("#outpatientName").val("请选择");
};

PerformanceTable.store = function () {
    $("#outpatientNameValue").val($("#outpatientName").val());
    PerformanceTable.search();
};

PerformanceTable.request = function () { // 初始化数据请求
    if ($('#outpatientNameValue').val() != "") {
        $("#outpatientName option[value='" + $('#outpatientNameValue').val() + "']").attr("selected", "selected");
    }
};

$("#doctorTcSearchAlls", parent.document).click(function () { // 主页搜索
    PerformanceTable.search();
});

PerformanceTable.total = function () {
    var reach = parent.$("#doctorTcInputPublics").val();
    var startRechargeTime = $("#startRechargeTime").val();
    var endRechargeTime = $("#endRechargeTime").val();
    var oneChannel = multipleSelection($('#oneChannel').val());
    var twoChannel = multipleSelection($('#twoChannel').val());
    var thrChannel = multipleSelection($('#thrChannel').val());
    var oneMedium = multipleSelection($('#oneMedium').val());
    var twoMedium = multipleSelection($('#twoMedium').val());
    var thrMedium = multipleSelection($('#thrMedium').val());
    var scarchiving = multipleSelection($('#scarchiving').val());
    var tcName = multipleSelection($('#tcName').val());
    var isDoName = $('#isDoName').val();
    var treatmentUnit = multipleSelection($('#treatmentUnit').val());
    var customerStatus = multipleSelection($('#customerStatus').val());
    var outpatientName = $('#outpatientNameValue').val();
    $.ajax({
        type: "GET",
        url: Feng.ctxPath + "/performance/total",
        data: {
            reach: reach,
            startRechargeTime: startRechargeTime,
            endRechargeTime: endRechargeTime,
            oneChannel1: oneChannel,
            twoChannel1: twoChannel,
            thrChannel1: thrChannel,
            oneMedium1: oneMedium,
            twoMedium1: twoMedium,
            thrMedium1: thrMedium,
            scarchiving1: scarchiving,
            tcName1: tcName,
            isDoName1: isDoName,
            treatmentUnit1: treatmentUnit,
            customerStatus1: customerStatus,
            outpatientName: outpatientName
        },
        dataType: "json",
        success: function (data) {
            $("#hospitalPerformance").html(data.hospitalPerformance);  //全院业绩
            $("#sectionPerformance").html(data.sectionPerformance);   //科室业绩
            $("#number").html(data.number);   //交易人数
        }
    });
};

PerformanceTable.naturalDaySearch = function () {
    $("#startRechargeTime").val(setDateTime(new Date(),0) + " " + "00:00:00");
    $("#endRechargeTime").val(setDateTime(new Date(),0) + " " + "23:59:59");
    $(".timeBox > div button").eq(0).addClass('actionClick').siblings().removeClass('actionClick');
    PerformanceTable.search();
};

PerformanceTable.settleAccounts = function () {
    var AccountsTime = $("#sttleAccountsTime").val();
    $("#startRechargeTime").val(setDateTime(new Date(),1) + " " + AccountsTime);
    $("#endRechargeTime").val(setDateTime(new Date(),0) + " " + AccountsTime);
    $(".timeBox > div button").eq(1).addClass('actionClick').siblings().removeClass('actionClick');
    PerformanceTable.search();
};

function phonePrivileges(){   //隐藏手机号
    var phonePrivileges=parent.$("#phonePrivileges").val();
    if(phonePrivileges == 2){
        $('#PerformanceTable').bootstrapTable('hideColumn', 'customerPhone');
    }
}

var parseParam = function (param, key) {
    var paramStr = "";
    if (param instanceof String || param instanceof Number || param instanceof Boolean) {
        paramStr += "&" + key + "=" + param;
    } else {
        $.each(param, function (i) {
            var k = key == null ? i : key + (param instanceof Array ? "[" + i + "]" : "." + i);
            paramStr += '&' + parseParam(this, k);
        });
    }
    return paramStr.substr(1);
};
PerformanceTable.performanceTableExport = function () {
    var param = parseParam(PerformanceTable.formParams());
    location.href = Feng.ctxPath + '/performance/performanceTableExport?' + param;
};

$(function () {
    var defaultColunms = PerformanceTable.initColumn();
    var table = new BSTable(PerformanceTable.id, "/performance/list", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(PerformanceTable.formParams());
    PerformanceTable.table = table.init();
    PerformanceTable.request();
    PerformanceTable.naturalDaySearch();
    checkDate = function () {PerformanceTable.search()};
    $(document).on('click', '#laydate_clear,#laydate_today,#laydate_ok', function () {PerformanceTable.search()});
    $("#oneChannel").change(function () {$("#twoChannel").val('请选择');$("#thrChannel").val('请选择');PerformanceTable.search()});
    $("#twoChannel").change(function () {$("#thrChannel").val('请选择');PerformanceTable.search()});
    $("#thrChannel").change(function () {PerformanceTable.search()});
    $("#oneMedium").change(function () {$("#twoMedium").val('请选择');$("#thrMedium").val('请选择');PerformanceTable.search()});
    $("#twoMedium").change(function () {$("#thrMedium").val('请选择');PerformanceTable.search()});
    $("#thrMedium").change(function () {PerformanceTable.search()});
    phonePrivileges();
});